package work.chief.servlet;

import work.chief.dao.OrdersDao;
import work.chief.dao.ProductDao;
import work.chief.dao.TrolleyDao;
import work.chief.dao.impl.OrdersDaoImpl;
import work.chief.dao.impl.ProductDaoImpl;
import work.chief.dao.impl.TrolleyDaoImpl;
import work.chief.entity.Orders;
import work.chief.entity.User;
import work.chief.service.OrdersService;
import work.chief.service.TrolleyService;
import work.chief.service.impl.OrdersServiceImpl;
import work.chief.service.impl.TrolleyServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "ordersServlet",urlPatterns = "/ordersServlet")
public class OrdersServlet extends BaseServlet {
    private TrolleyService trolleyService=new TrolleyServiceImpl();
    private TrolleyDao trolleyDao=new TrolleyDaoImpl();
    private ProductDao productDao=new ProductDaoImpl();
    private OrdersDao ordersDao=new OrdersDaoImpl();
    private OrdersService ordersService =new OrdersServiceImpl();

    public void addOrders(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        PrintWriter pw= response.getWriter();

        String tid = request.getParameter("tid");
        User user = (User) request.getSession().getAttribute("user");
        int uid=user.getId();
        int i = ordersService.addOrders(uid, tid,request);
        if (i>0){
            pw.print(true);
        }else {
            pw.print(false);
        }
//        request.getRequestDispatcher("trolley.jsp").forward(request,response);
        pw.close();

    }

    public  void selectOrders(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        User user = (User)request.getSession().getAttribute("user");
        int uid=user.getId();
        List<Orders> ordersList = ordersService.selectByUid(uid);
        request.setAttribute("ordersList",ordersList);
        request.getRequestDispatcher("orders_history.jsp").forward(request,response);
    }
}
